<?php

class StudentPlace_SemesterCompareController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function getDataAction()
    {
        $this->getHelper('layout')->disableLayout();
        $tempArr = explode('-', $this->_getParam('yearSemester'));
        $year = (int) $tempArr[0];
        $semester = (int) $tempArr[1] ;
        $grade = (int) $this->_getParam('grade');

        // 上學期
        if ($semester == 1) {
            $oldYear = $year-1;
            $oldSemester = 2;
            $oldGrade = $grade - 1;
        }
        else {
            $oldYear = $year;
            $oldSemester = 1;
            $oldGrade = $grade;
        }

        $q = Doctrine_Query::create()
        ->select('a.sn, a.name, b.grade AS grade, b.class_sn AS class_sn, b.number AS number')
        ->from('Student_Model_Student a')
        ->leftJoin('a.Student_Model_StudentSemester b')
        ->where('a.study_condition=?' , 0)
        ->andWhere('b.year=?', $year)
        ->andWHere('b.semester=?', $semester)
        ->andWhere('b.grade=?', $grade)
        ->orderBy('b.class_sn, b.number') ;

        $res = $q->FetchArray();

        $currentArray = $oldArray = array();

        foreach ($res as $row)
        $currentArray[$row['sn']]  = $row;

        $q = Doctrine_Query::create()
        ->select('sn, student_sn, grade, class_sn, number')
        ->From('Student_Model_StudentSemester')
        ->Where('year=?', $oldYear)
        ->andWHere('semester=?', $oldSemester)
        ->andWhere('grade=?', $oldGrade);

        $res = $q->fetchArray();
        foreach ($res as $row)
        $oldArray[$row['student_sn']] = $row;

        $this->view->year = $year;
        $this->view->semester = $semester;
        $this->view->oldYear = $oldYear;
        $this->view->oldSemester = $oldSemester;
        $this->view->currentArray = $currentArray;
        $this->view->oldArray = $oldArray;
    }

}

